﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using MINERP.BLL.InfoConfig;
using MINERP.MODEL.InfoConfig;

namespace MINERP.InfoConfig
{
    public partial class FrmPhotoSize : Form
    {
        private ProductEntity _productEntity;

        ProductBLL _bll = new ProductBLL();
        public int _productId;
        bool _isEdit = false;
        public List<PhotoSize_Info> _photoSizeList = new List<PhotoSize_Info>();

        public FrmPhotoSize(int productId)
        {
            InitializeComponent();
            _productId = productId;
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {

        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void FrmPhotoSize_Load(object sender, EventArgs e)
        {
            try
            {
                comboBoxUnit.SelectedIndex = 0;
                _productEntity = _bll.GetProductInfoById(_productId);
                _photoSizeList = _productEntity.photoSizeInfoList;
                RefreshDataSource();
                lbProductName.Text = _productEntity.ProductName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void btnAddSize_Click(object sender, EventArgs e)
        {
            double widthMAX =0;
            double widthMIN =0;
            double heightMAX =0;
            double heightMIN =0;
            if(comboBoxUnit.SelectedIndex==0)
            {
             widthMAX = Convert.ToDouble(txtWidthMAX.Text);
             widthMIN =Convert.ToDouble(txtWidthMIN.Text);
             heightMAX = Convert.ToDouble(txtHeightMAX.Text);
             heightMIN =Convert.ToDouble(txtHeightMIN.Text);
            }
            else
            {
             widthMAX = Convert.ToDouble(txtWidthMAX.Text)/2.54;
             widthMIN =Convert.ToDouble(txtWidthMIN.Text)/2.54;
             heightMAX = Convert.ToDouble(txtHeightMAX.Text)/2.54;
             heightMIN =Convert.ToDouble(txtHeightMIN.Text)/2.54;
            }
            
            int pageNum =Convert.ToInt32(txtPageNum.Text);
            string describtion =txtDescription.Text;

            PhotoSize_Info photoSize = new PhotoSize_Info
            {
                WidthMAX = widthMAX,
                WidthMIN = widthMIN,
                HeightMAX = heightMAX,
                HeightMIN = heightMIN,
                PageNum = pageNum,
                Describe = describtion

            };
            if (!ExsitSize(_productId, photoSize))
            {
                _photoSizeList.Add(photoSize);
                RefreshDataSource();
            }
            else
            {
                MessageBox.Show("该尺寸已经存在！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void RefreshDataSource()
        {
            gcSize.DataSource = _photoSizeList;
            gcSize.RefreshDataSource();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _bll.SetProductSize(_productId, _photoSizeList);
                MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        /// <summary>
        /// 是否存在这一尺寸的产品
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="photosize"></param>
        /// <returns></returns>
        private bool ExsitSize(int productId, PhotoSize_Info photosize)
        {
            return _photoSizeList.Exists(p=>p.HeightMAX==photosize.HeightMAX
                    &&p.HeightMIN ==photosize.HeightMIN
                    &&p.WidthMAX ==photosize.WidthMAX
                    &&p.WidthMIN ==photosize.WidthMIN);
        }

        private void contextMenuStrip1_Click(object sender, EventArgs e)
        {
            PhotoSize_Info ps = gvSize.GetFocusedRow() as PhotoSize_Info;
            if (ps != null)
            {
                _photoSizeList.Remove(ps);
                RefreshDataSource();
            }
            else
            {
                MessageBox.Show("请选中要删除的行！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void simpleButtonDelete_Click(object sender, EventArgs e)
        {
            PhotoSize_Info ps = gvSize.GetFocusedRow() as PhotoSize_Info;
            if (ps != null)
            {
                _photoSizeList.Remove(ps);
                RefreshDataSource();
            }
            else
            {
                MessageBox.Show("请选中要删除的行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            PhotoSize_Info ps = gvSize.GetFocusedRow() as PhotoSize_Info;
            if (ps != null)
            {
                _photoSizeList.Remove(ps);
                RefreshDataSource();
            }
            else
            {
                MessageBox.Show("请选中要删除的行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void radioButtonCm_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void comboBoxUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
             PhotoSize_Info ps = gvSize.GetFocusedRow() as PhotoSize_Info;
             if (ps != null)
             {
                 if (comboBoxUnit.SelectedIndex == 0)
                 {
                     txtWidthMIN.Text = ps.WidthMIN.ToString(); ;
                     txtWidthMAX.Text = ps.WidthMAX.ToString();
                     txtHeightMIN.Text = ps.HeightMIN.ToString();
                     txtHeightMAX.Text = ps.HeightMAX.ToString();
                 }
                 else
                 {
                     txtWidthMIN.Text = ps.WidthCMMIN.ToString(); ;
                     txtWidthMAX.Text = ps.WidthCMMAX.ToString();
                     txtHeightMIN.Text = ps.HeightCMMIN.ToString();
                     txtHeightMAX.Text = ps.HeightCMMAX.ToString();
                 }
                 
             }
        }

        private void gvSize_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
               
                PhotoSize_Info ps = (PhotoSize_Info)gvSize.GetRow(e.RowHandle);
                if (ps != null)
                {
                    _isEdit = true;
                  
                    if (comboBoxUnit.SelectedIndex == 0)
                    {
                        txtWidthMIN.Text = ps.WidthMIN.ToString(); ;
                        txtWidthMAX.Text = ps.WidthMAX.ToString();
                        txtHeightMIN.Text = ps.HeightMIN.ToString();
                        txtHeightMAX.Text = ps.HeightMAX.ToString();
                       
                    }
                    else
                    {
                        txtWidthMIN.Text = ps.WidthCMMIN.ToString(); ;
                        txtWidthMAX.Text = ps.WidthCMMAX.ToString();
                        txtHeightMIN.Text = ps.HeightCMMIN.ToString();
                        txtHeightMAX.Text = ps.HeightCMMAX.ToString();
                    }
                    txtPageNum.Text = ps.PageNum.ToString();
                    txtDescription.Text = ps.Describe;
                }

            }
        }
    }
}
